<?php
// +----------------------------------------------------------------------
// | 查询页面
// +----------------------------------------------------------------------
// | Author: Helly  QQ:15655663
// +----------------------------------------------------------------------
// | Date: 2015/03/10 18:13
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;
use Think\Controller;

class SearchController extends Base {

    //查询结果
    public function index(){

        $get=I('get.');
        $type=I('get.t');
        $keyword = \Input::getVar(I('get.keyword'));

        if(empty($type)){
            $this->_empty();
        }
        if(empty($keyword)){
            $this->error('亲爱的，你似乎没有输入需要搜索的关键词...');
        }
        $url='&keyword='.$keyword.'&t='.$type;

        if($type==1){
            //中间广告位
            $this->assign('midad',parent::getAdvert(2,1,0,1));


            $worksModel=M('Works');
            $where['status']=array('EQ',true);
            $where['title'] = array("LIKE", "%{$keyword}%");

            $count = $worksModel->where($where)->count();
            $page = $this->Pages($count, 30);
            $pageurl='?page='.urlencode('[PAGE]');
            $page->url     = __APP__.'/search/'.$pageurl.$url;

            $Model=new Model();
            $list=$Model
                ->table(array(
                        C('DB_PREFIX').'works'=>'W',
                        C('DB_PREFIX').'user'=>'U',
                    )
                )
                ->where("W.userid=U.userid and W.status=true and W.title like '%".$keyword."%'")
                ->field('W.title,W.type,W.content,W.goodnum,W.msgnum,W.workid,W.create_time,W.image,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,U.ifrz')
                ->order('msgnum DESC')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
            foreach ($list as $key=>$value) {
                $list[$key]['dignum']=M('Dig')->where(array('status'=>true,'infoid'=>$value['workid'],'userid'=>$this->userid))->count();
            }

            $this->assign('keywords',$keyword);
            $this->assign("list", $list);
            $this->assign("Page", $page->show());
            $this->display('works');


        }else{

            $userModel=M('User');
            $where['status']=array('EQ',true);
            $where['nickname'] = array("LIKE", "%{$keyword}%");

            $count = $userModel->where($where)->count();
            $page = $this->Pages($count, 40);
            $pageurl='?page='.urlencode('[PAGE]');
            $page->url     = __APP__.'/search/'.$pageurl.$url;
            $list=$userModel
                ->where($where)
                ->field('userid,uavatar,openuid,nickname,worksnum,spnum,fansnum')
                ->order('worksnum DESC')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
            foreach($list as $key=>$value){
                $list[$key]['worknums']=$value['worksnum']+$value['spnum'];
                if(M('UserFans')->where(array('userid'=>$value['userid'],'myid'=>$this->userid,'status'=>true))->count()>0){
                    $list[$key]['isfans']=true;
                }
            }
            $this->assign("list", $list);
            $this->assign("Page", $page->show());

            $this->display('user');
        }


    }


    /**
     * 更新作品数
     */
    public function upnum(){
        $isadmin=I('get.isadmin');
        if($isadmin=='helly'){
            $user=M('User')->field(array('userid','worksnum','spnum','nickname'))->select();
            foreach($user as $k=>$v){

                $workscount=M('Works')->where(array('userid'=>$v['userid'],'status'=>true))->count();
                $spnum=M('Suipai')->where(array('userid'=>$v['userid'],'status'=>true))->count();
             //   echo $workscount.'----'.$spnum.'---'.$v['nickname']."<br>";
                M('User')->where(array('userid'=>$v['userid']))->data(array('worksnum'=>$workscount,'spnum'=>$spnum))->save();

            }
        }else{
            $this->_empty();
        }




    }

}